/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.mediation.core
{
	import org.etherframework.IEther;
	import org.etherframework.common.mediation.IMediationBind;
	import org.etherframework.common.mediation.IMediationExecute;
	import org.etherframework.common.mediation.IMediations;
	import org.etherframework.common.mediation.facade.IMediations;
	import org.etherframework.common.mediation.facade.IMediationsFacade;
	import org.etherframework.core.bridge.IBridgeFacades;
	import org.etherframework.core.module.IModulePartFacade;
	import org.etherframework.mediation.IRUID;
	import org.etherframework.mediation.Mediations;
	import org.etherframework.mediation.manager.IMediationManager;
	
	[ExcludeClass]
	// TODO: Refactor.
	public class MediationsFacade implements IMediationsFacade, IMediationExecute
	{
		private var _owner:IBridgeFacades;
		private var _part:Mediations;
		private var _manager:IMediationManager;
		private var _info:MediationData;
		private var _ether:IEther;
		private var _context:String;
		private var _bind:MediationBind;
		
		public function MediationsFacade(owner:IBridgeFacades, part:Mediations, 
										 manager:IMediationManager)
		{
			_owner = owner;
			_part = part;
			_manager = manager;
			
			init();
		}
		
		public function prepare(ether:IEther):IModulePartFacade
		{
			clean();
			_ether = ether;
			
			return this;
		}
		// TODO: Think about using lexical scope for binding methods instead of proxing.
		public function get bind():IMediationBind
		{
			_info.clear();
			_info.isBinding = true;
			_info.context = _context;
			
			return _bind.prepare(_ether, _info, true);
		}
		
		public function get unbind():IMediationBind
		{
			_info.clear();
			_info.isBinding = true;
			_info.context = _context;
			
			return _bind.prepare(_ether, _info, false);
		}
		
		public function get execute():IMediationExecute
		{
			_info.clear();
			_info.isExecuting = true;
			_info.context = _context;
			return this;
		}
		
		public function binding(term:*):*
		{
			clean();
			return term;
		}
		
		public function clear():void
		{
			_manager.clearMediations(_ether);
		}
		
		public function contextual(value:String):org.etherframework.common.mediation.facade.IMediations
		{
			_context = value;
			
			return this;
		}
		
		private function init():void
		{
			_info = _manager.mediationData;
			_bind = new MediationBind(_manager);
		}
		
		private function clean():void
		{
			_info.clear();
			_ether = null;
			_context = null;
		}
	}
}